/**
 * Created by oneminute on 7/28/14.
 */

var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();
var httpMocks = require("../lib/http-mock");
var async = require("async");
var path = require("path");
var goods = require("../../routes/goods");
var userFilter = require("../../routes/userfilter");

describe("测试routes/goods模块。", function () {

    var user = null;
    before(function (done) {
        var uid = lz.Utils.getUUID("u_");
        var commodities = [];
        for (var i = 0; i < 10; i++) {
            commodities.push({
                id: lz.Utils.getUUID("c_"),
                uid: uid,
                description: "测试商品描述" + i,
                price: 102.18
            });
        }
        user = new lz.User({
            id: uid,
            name: lz.Utils.getRandName(),
            commodities: commodities,
            inbusiness: true
        });
        user.save(function (_err, _user) {
            done();
        });
    });

    it("正常获取商品。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/goods?uid=' + lz.Utils.getUUID("t_") + "&gid=" + user.commodities[4].id
        });
        var response = httpMocks.createExpressResponse();

        var getFn = goods.get.bind(null, request, response, {}, function (_err, _user, _args) {
            testutils.check(done, function () {
                assert.equal(_args.description, user.commodities[4].description);
            });
        });
        userFilter(request, response, getFn);
    });

    it("未传入有效的cid。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/goods?uid=' + lz.Utils.getUUID("t_") + "&gid=" + lz.Utils.getUUID("c_")
        });
        var response = httpMocks.createExpressResponse();

        var getFn = goods.get.bind(null, request, response, {}, function (_err, _user, _args) {
            testutils.check(done, function () {
                assert.equal(_err.code, lz.Config.errors.REQUEST_NO_COMMODITY.code);
            });
        });
        userFilter(request, response, getFn);
    });

    after(function (done) {
        user.delete(function () {
            //lz.ImageManager.removePhotos(photo, function () {
            done();
            //});
        });
    });
});